﻿using System;
using Core.Library;
using DataObject;
using DataObject.Members;
using DataObject.Ref;

namespace Event
{
    public partial class List2 : System.Web.UI.Page
    {
        #region variables, constants, properties
        private const int _pageSize = 32;
        protected int _round;
        protected string _order;
        protected bool _photos;
        protected string[] _linkRound;
        private const string NO_ALBUM = @"<div class=""no-album"">Chưa có thí sinh nào upload ảnh vòng này</div>";
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!RefCache.Ref_GetStartContestStatus()) Response.Redirect("/");

            _round = Utils.GetIdFromRequestString(Utils.Round, 1);
            _photos = Utils.GetIdFromRequestString(Utils.Photos, false);
            string keyword = Utils.GetIdFromRequestString(Utils.Keyword, string.Empty);
            _order = Utils.GetIdFromRequestString(Utils.Order, "qua-vong");
            int pi = Utils.GetIdFromRequestString(Utils.PageIndex, SettingValues.PageIndex);
            _linkRound = new string[3];
            if (!IsPostBack)
            {
                var currentRound = RefCache.Ref_GetCurrentRound();
                phRound2.Visible = currentRound > (int)MemberRound.Round1;
                phRound3.Visible = currentRound > (int)MemberRound.Round2;
                phFollowVote.Visible = RefCache.Ref_GetVoteDisplayStatus() || _round < RefCache.Ref_GetCurrentRound();
                phIsWin.Visible = _round < (int)MemberRound.Round2;
                phMoiNhat.Visible = _round == (int)MemberRound.Round1;
                if (_photos)
                {
                    #region list photo album

                    _order = string.Empty;
                    MetaKeyword.AddMetaTag(Page, "Photo | " + Const.Meta_Title, Const.Meta_Sapo);
                    MetaKeyword.AddFacebookMetaTag(Page, "Photo | " + Const.Meta_Title, Const.Meta_Sapo, Const.SITE_URL, Const.Meta_Image);

                    this.ctrListAllPhoto._round = _round;
                    this.ctrListAllPhoto.Visible = true;
                    _linkRound[0] = "/photo/vong-1.htm";
                    _linkRound[1] = "/photo/vong-2.htm";
                    _linkRound[2] = "/photo/vong-3.htm";
                    #endregion
                }
                else
                {
                    #region list album

                    MetaKeyword.AddMetaTag(Page, "Bài dự thi | " + Const.Meta_Title, Const.Meta_Sapo);
                    MetaKeyword.AddFacebookMetaTag(Page, "Bài dự thi | " + Const.Meta_Title, Const.Meta_Sapo, Const.SITE_URL, Const.Meta_Image);

                    {
                        _linkRound[0] = "/bai-du-thi/vong-1/qua-vong.htm";
                        _linkRound[1] = "/bai-du-thi/vong-2/ten.htm";
                        _linkRound[2] = "/bai-du-thi/vong-3/moi-nhat.htm";


                        if (_order == "qua-vong")
                        {
                            var dt = MembersCache.Members_GetList_Prev2(_round, keyword, pi, _pageSize);
                            this.ctrListAlbum.Visible = this.ctrListAlbum.DataBind(dt, _round);
                            if (dt != null && dt.Rows.Count > 0)
                            {
                                ctrPaging.LinkSite = "bai-du-thi/";
                                if (keyword != string.Empty)
                                {
                                    ctrPaging.IsSearch = true;
                                    ctrPaging.Round = _round;
                                    ctrPaging.Keyword = keyword;
                                    ctrPaging.LinkSite += "tim-kiem";
                                }
                                else
                                {
                                    ctrPaging.LinkSite += "vong-" + _round + "/";
                                    if (_order != string.Empty) ctrPaging.LinkSite += _order + "/";
                                    else ctrPaging.LinkSite += "qua-vong/";
                                }
                                ctrPaging.PageSize = _pageSize;
                                var total = MembersCache.Members_GetList_Count_Prev2(_round, keyword);
                                ctrPaging.SumPage = (int)Math.Ceiling((decimal)total / _pageSize);
                                ctrPaging.Visible = ctrPaging.Padding(pi);
                            }
                        }
                        else
                        {
                            var ordertype = 0;
                            if (Constants.ListOrderType.ContainsKey(_order))
                                ordertype = Constants.ListOrderType[_order];

                            var dt = MembersCache.Members_GetList(_round, ordertype, keyword, pi, _pageSize);
                            this.ctrListAlbum.Visible = this.ctrListAlbum.DataBind(dt, _round);
                            if (dt != null && dt.Rows.Count > 0)
                            {
                                ctrPaging.LinkSite = "bai-du-thi/";
                                if (keyword != string.Empty)
                                {
                                    ctrPaging.IsSearch = true;
                                    ctrPaging.Round = _round;
                                    ctrPaging.Keyword = keyword;
                                    ctrPaging.LinkSite += "tim-kiem";
                                }
                                else
                                {
                                    ctrPaging.LinkSite += "vong-" + _round + "/";
                                    if (_order != string.Empty) ctrPaging.LinkSite += _order + "/";
                                    else ctrPaging.LinkSite += "moi-nhat/";
                                }
                                ctrPaging.PageSize = _pageSize;
                                var total = MembersCache.Members_GetList_Count(_round, ordertype, keyword);
                                ctrPaging.SumPage = (int)Math.Ceiling((decimal)total / _pageSize);
                                ctrPaging.Visible = ctrPaging.Padding(pi);
                            }
                            else if (String.IsNullOrWhiteSpace(keyword))
                            {
                                ltrNoAlbum.Text = NO_ALBUM;
                                ltrNoAlbum.Visible = true;
                            }
                        }
                    }
                    #endregion
                }

                #region Quang cao flash
                {
                    ltrAdvLeft.Text = Utils.BuildFlash("advLeft", "/images/banner-bottom-460x120.swf", string.Empty, 460, 120);
                }
                #endregion
            }
        }
    }
}